package com.plwf.lake.service.impl;

import com.plwf.lake.entity.ClassInfo;
import com.plwf.lake.entity.Student;
import com.plwf.lake.mapper.ClassInfoMapper;
import com.plwf.lake.mapper.StudentMapper;
import com.plwf.lake.service.ClassInfoService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class ClassInfoServiceImpl implements ClassInfoService{
    @Resource
    private ClassInfoMapper mapper;
    @Resource
    private StudentMapper dao;

    @Override
    public List<ClassInfo> query(Map<String, Object> map) {

        // id 查询所有已预约的科目信息 itemList
        //
        //.mapper.selectReverse
        List<ClassInfo> classList = this.mapper.select(map);
        if (map.get("stuId") != null ) {
            Map<String,Object> stuMap = new HashMap<>();
            stuMap.put("stuId",map.get("stuId"));
            List<Student> stuList = dao.selectReverse(stuMap);
            if(stuList.size() != 0) {
                int count = 0;
                    for (ClassInfo allClass : classList) {
                        for (ClassInfo stuClass : stuList.get(0).getClassInfoList()) {
                            if (stuClass.getClassId().equals(allClass.getClassId())) {
                                allClass.setIsReve("1");
                                classList.set(count,allClass);
                                break;
                            }
                            classList.set(count,allClass);
                        }
                    count++;
                }
            }
        }
        return classList;
    }

    @Override
    public boolean enable(Integer id, Integer status) {
        ClassInfo  classInfo = new ClassInfo();
        classInfo.setClassId(id);
        classInfo.setStatus(status);
        return this.mapper.enable(classInfo);
    }

    @Override
    @Transactional(propagation= Propagation.REQUIRED,rollbackFor=SQLException.class)
    public boolean remove(Integer classId) {
        return this.mapper.delete(classId);
    }

    @Override
    @Transactional(propagation= Propagation.REQUIRED,rollbackFor=SQLException.class)
    public boolean save(ClassInfo classInfo) {
        if(classInfo.getClassId()!=null){
           return this.mapper.update(classInfo);
        }else {
           return this.mapper.insert(classInfo);
        }
    }

    @Override
    public ClassInfo find(Integer classId) {
        return this.mapper.selectOne(classId);
    }

    @Override
    public ClassInfo findRes(Integer classId) {
        return this.mapper.seleRes(classId);
    }

    @Override
    public boolean findClass(ClassInfo bean) {
        ClassInfo classInfo = mapper.selClass(bean);
        if (classInfo != null)
            return true;
        else
            return false;
    }
}
